package leetcode.每日一题.优先队列;

import leetcode.util.CommonUtils;
import org.junit.Test;

import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * @author ：zsy
 * @date ：Created 2023/1/2 8:37 PM
 * @description：
 */
public class 积压订单中的订单总数 {
    @Test
    public void test() {
        CommonUtils.arrTransfer("[[14,16,1],[14,12,0],[18,18,1],[15,20,0],[26,8,0],[18,19,0],[28,3,1],[2,27,0],[3,28,0]]");
        // System.out.println(new Solution().getNumberOfBacklogOrders(new int[][]{{10, 5, 0}, {15, 2, 1}, {25, 1, 1}, {30, 4, 0}}));
        // System.out.println(new Solution().getNumberOfBacklogOrders(new int[][]{{7,1000000000,1},{15,3,0},{5,999999995,0},{5,1,1}}));
        // System.out.println(new Solution().getNumberOfBacklogOrders(new int[][]{{26,7,0},{16,1,1},{14,20,0},{23,15,1},{24,26,0},{19,4,1},{1,1,0}}));
        // System.out.println(new Solution().getNumberOfBacklogOrders(new int[][]{{1,29,1},{22,7,1},{24,1,0},{25,15,1},{18,8,1},{8,22,0},{25,15,1},{30,1,1},{27,30,0}}));
        System.out.println(new Solution().getNumberOfBacklogOrders(new int[][]{{14,16,1},{14,12,0},{18,18,1},{15,20,0},{26,8,0},{18,19,0},{28,3,1},{2,27,0},{3,28,0}}));

    }

    class Solution {
        private int MOD = 1000000007;
        public int getNumberOfBacklogOrders(int[][] orders) {
            PriorityQueue<int[]> buyBacklog = new PriorityQueue<>((a, b) -> b[0] - a[0]);
            PriorityQueue<int[]> sellBacklog = new PriorityQueue<>(Comparator.comparingInt(a -> a[0]));
            for (int[] order : orders) {
                int price = order[0];
                int amount = order[1];
                int orderType = order[2];
                if (orderType == 0) { // buy订单
                    while (!sellBacklog.isEmpty()) {
                        int[] minSellOrder = sellBacklog.peek();
                        if (minSellOrder[0] > price) {
                            break;
                        }
                        if (minSellOrder[1] > amount) {
                            minSellOrder[1] -= amount;
                            amount = 0;
                            break;
                        } else {
                            amount -= minSellOrder[1];
                            sellBacklog.poll();
                        }
                    }
                    if (amount != 0) { // 塞进buy pq
                        order[1] = amount;
                        buyBacklog.add(order);
                    }
                } else { // sell订单
                    while (!buyBacklog.isEmpty()) {
                        int[] maxBuyOrder = buyBacklog.peek();
                        if (maxBuyOrder[0] < price) {
                            break;
                        }
                        if (maxBuyOrder[1] > amount) {
                            maxBuyOrder[1] -= amount;
                            amount = 0;
                            break;
                        } else {
                            amount -= maxBuyOrder[1];
                            buyBacklog.poll();
                        }
                    }
                    if (amount != 0) { // 塞进buy pq
                        order[1] = amount;
                        sellBacklog.add(order);
                    }
                }
            }
            int ans = 0;
            while (!buyBacklog.isEmpty()) {
                int[] poll = buyBacklog.poll();
                ans = (poll[1] + ans) % MOD;
            }
            while (!sellBacklog.isEmpty()) {
                int[] poll = sellBacklog.poll();
                ans = (ans + poll[1]) % MOD;
            }
            return ans % MOD;
        }
    }
}
